<?php
/**
 * 如果按php原来循环去请求的话，至少需要6s
 * 采用进程的形式，只需要1s就可以执行完这6个请求
 */

echo "start-time" . date('Y-m-d H:i:s') . PHP_EOL;
$urls = [
    'http://www.baidu.com',
    'http://baidu.com?search=6666',
    'http://baidu.com?search=9999',
    'http://baidu.com?search=1111',
    'http://baidu.com?search=0000',
    'http://baidu.com?search=00010',
];
$workers = [];

for ($i = 0; $i < 6; $i++){
    //子进程
    $process = new swoole_process(function ()use($i,$urls){
        $content = curlData($urls[$i]);
        echo $content . PHP_EOL;
    },true);
    $pid = $process->start();
    $workers[$pid] = $process;
}

//从管道中读取
foreach ($workers as $process){
    echo $process->read();
}

function curlData($url)
{
    sleep(1);
    return $url . '-success' . PHP_EOL;
}
echo "end-time" . date('Y-m-d H:i:s') . PHP_EOL;
